﻿if (CategoriesArray.tool) {
	//create List component
	var ListToolsField:TextField = new TextField();
	var ListTools:List = new List();
	placeComponent(ListTools, ListToolsField, "Choose Tool", datagridPadding, tool.backg);

	//Create "Select Tool" list
	var myDataProvider:DataProvider = new DataProvider();
	myDataProvider.addItem({label:"List Words", url:""});
	myDataProvider.addItem({label:"Word Cloud", url:""});
	myDataProvider.addItem( { label:"Get Concordance", url:"" } );
	myDataProvider.addItem( { label:"Collocating Words" , url:"" } );
	ListTools.dataProvider=myDataProvider;
	ListTools.selectedIndex=0;
	ListTools.getItemAt(0).selectable=false;
	ListTools.height=myDataProvider.length*20;
	ListTools.addEventListener(Event.CHANGE, function(){toolChosen()});
}
function toolChosen() {
		if (tool.backg.contains(patternSelect)) {
			tool.backg.removeChild(patternSelect);
		}
		if (tool.backg.contains(patternField)) {
			tool.backg.removeChild(patternField);
		}
		if (tool.backg.contains(runTool)) {
			tool.backg.removeChild(runTool);
		}
		CategoriesArray["tool"].title="Tool : "+ListTools.selectedItem.label;
		tool.catBar.catTXT.text=CategoriesArray["tool"].title;
		if (CategoriesArray.options) {
			slidingSetup(CategoriesArray["options"].position);
		} 
		else if (ListTools.selectedItem.label=="Old Collocating Words" || ListTools.selectedItem.label=="Old Get Concordance" || ListTools.selectedItem.label=="Collocating Words" || ListTools.selectedItem.label=="Get Concordance") {
			//Pattern Select
			
			placeComponent(patternSelect, patternField, "Pattern", placeBelow(ListTools), tool.backg);
			patternField.multiline=false;
			patternSelect.height=25;
			setLabelText(toolInfo, "<font size='+1'>After entering the word for analysis, click <font size='+2'>'Results'</font>.</font>", placeBelow(patternField), tool.backg);
			runTool = new Button();
 			runTool.width=toolWidth-datagridPadding*2;
 			runTool.move(datagridPadding+(widthDif/2),patternSelect.y+patternSelect.textHeight+runTool.height+3);
 			runTool.label = "Results";
			tool.backg.addChild(runTool);
 			runTool.addEventListener(MouseEvent.CLICK, function() {
				slidingSetup (CategoriesArray["Results"].position);
				NavigateToURLcss();
			});
			patternSelect.addEventListener(KeyboardEvent.KEY_DOWN, function(event:KeyboardEvent){;
			trace (event.keyCode);
			if (event.keyCode==13) {
			slidingSetup (CategoriesArray["Results"].position);
			NavigateToURLcss();
		};
		});
	} 
		else if (ListTools.selectedItem.label=="Old List Words" || ListTools.selectedItem.label=="Old Word Cloud"  || ListTools.selectedItem.label=="Word Cloud" || ListTools.selectedItem.label=="List Words") {
			setLabelText(toolInfo, "<font size='+1'>Click <font size='+2'>'Results'</font> below.</font>", placeBelow(ListTools), tool.backg);
			slidingSetup(CategoriesArray["Results"].position);
			NavigateToURLcss();
		}
	}
if (CategoriesArray.options) {
//HTML TAG Select
var TagField:TextField = new TextField();
var htmlTagSelect:TextArea = new TextArea();
placeComponent(htmlTagSelect, TagField, "HTML Tag", datagridPadding, options.backg);
//set default HTML Tag
htmlTagSelect.height=25;
htmlTagSelect.text=allOptions["htmlTag"];

//List Options
var ListOpField:TextField = new TextField();
var ListOp:List = new List();
placeComponent(ListOp, ListOpField, "List Options", placeBelow(htmlTagSelect), options.backg);
//Create "list Options" list
var ListOpData:DataProvider = new DataProvider();
ListOpData.addItem({label:"All words", code:"all"});
ListOpData.addItem({label:"Words matching pattern below", code: "patt"});
ListOpData.addItem({label:"Words in the list below", code:"find"});
ListOpData.addItem({label:"All words except stop list below", code: "stop"});
ListOp.dataProvider=ListOpData;
ListOp.selectedIndex=defaultListOp;
ListOp.height=ListOpData.length*20;

//Stop List
var stopListField:TextField = new TextField();
var stopList:TextArea = new TextArea();
placeComponent(stopList,stopListField, "Stop List", placeBelow(ListOp), options.backg);
//set default Stop List
stopList.height=25;
stopList.text=allOptions["stopList"];

//Sort Criteria
var sortCritField:TextField = new TextField();
var sortCrit:List = new List();
placeComponent(sortCrit, sortCritField, "Sort Criteria", placeBelow(stopList), options.backg);
//Create "Sort Criteria" list
var sortData:DataProvider = new DataProvider();
sortData.addItem({label:"By frequency", code:"2"});
sortData.addItem({label:"Alphabetically", code:"1"});
sortData.addItem({label:"By the order of appearance", code:"3"});
sortData.addItem({label:"By reversed alphabetic order", code:"4"});
sortCrit.dataProvider=sortData;
sortCrit.allowMultipleSelection=false;
sortCrit.selectedIndex=defaultSortCrit;
sortCrit.height=sortData.length*20;
}
if (CategoriesArray.Results) {
//cloudField1 = new TextField();
var Loading:MovieClip = new LoadAnimation();
Loading.x=toolWidth/2+widthDif/2;
Loading.y=toolHeight/2-40;
Loading.height=Loading.height*0.75;
Loading.width=Loading.width*0.75;
Loading.blendMode=BlendMode.DIFFERENCE;
Results.addChild(Loading);

}
if (CategoriesArray.about) {
//create List component
var aboutField1:TextField = new TextField();
aboutField1.wordWrap=true;
aboutField1.multiline=true;
var aboutText1:String= "<font size='+3' color='"+allOptions["header-color"]+"'>" 
+ link("TAToo","http://tada.mcmaster.ca/Main/FlashTAT") 
+ " is a Flash widget which calls basic text analysis tools from the " 
+ link("TAPoR","http://tapor.ualberta.ca/") 
+ " project. <br/><br/>TAToo was developed by " 
+ link("Peter Organisciak","http://www.porganized.com") 
+ " under the supervision of " 
+ link("Geoffrey Rockwell","http://geoffreyrockwell.com") 
+ ".<br/><br/>TAToo is part of the"
+ link("FlashTAT project", "http://tada.mcmaster.ca/Main/FlashTAT")
+ ".</font>";

setLabelText(aboutField1,aboutText1, datagridPadding, aboutCat.backg);
aboutField1.height = (toolHeight-(distanceBetween*totalCats+datagridPadding*2));
}
if (debug == true) {
var labelField:TextField = new TextField();
labelField.multiline=true;
labelField.wordWrap = true;
labelField.height=250;
labelField.background=true;
labelField.border=true;
labelField.width=toolWidth-datagridPadding*2;
labelField.x=datagridPadding+(widthDif/2);
labelField.y=datagridPadding;
labelField.antiAliasType=AntiAliasType.ADVANCED;
labelField.htmlText="";
addChild(labelField);
labelField.visible = false;


var debugText:Button = new Button();
debugText.width=toolWidth-datagridPadding*2;
debugText.move(datagridPadding + (widthDif / 2), labelField.y*2 + labelField.height );
debugText.label = "Change Text to Ulysses Text";
debugText.addEventListener(MouseEvent.CLICK, function() {
				htmlText="Ulysses Ulysses Ulysses Ulysses Ulysses Ulysses Ulysses Ulysses Ulysses Ulysses Thus railed Thersites, but Ulysses at and one would turn to his neighbour saying, \"Ulysses has done many a good thing ere now in fight and council, but he never did the Argives a better turn than when he stopped this fellow's mouth from prating further. He will give the kings no more of his insolence.\" Thus said the people. Then Ulysses rose, sceptre in hand, and Minerva in the likeness of a herald bade the people be still, that those who were far off might hear him and consider his council. He therefore with all sincerity and goodwill addressed them thu";
			});
addChild(debugText);
debugText.visible = false;

var alphabetButton:Button = new Button();
alphabetButton.width=toolWidth-datagridPadding*2;
alphabetButton.move(datagridPadding + (widthDif / 2), labelField.y*2 + labelField.height + debugText.height);
alphabetButton.label = "Change Text to Alphabet Text";
alphabetButton.addEventListener(MouseEvent.CLICK, function() {
				htmlText = "Airplane apple boat bicycle ball bath bubbles balloon. Baby blanket car cow cat crayons cake dog doctor deer drum duck eeyore elephant. Eggs flag frog fish goldfish goat glue horse hat ice cream insects island jack-in-the-box juice jammies kite keys lemon lake lion lollipop monkey mask music mushroom mail map nickel notes orange owl ostrich pigeon puppy piglet pig pumpkin pine cone quarter queen Roo robot rabbit rock star snail snake snow spider spoon tree table train tractor umbrella violin van world water wagon winnie the pooh xylophone x-ray yo-yo. Yellow yarn zebra zoo zipper airplane boat cake dog eeyore flag glue horse ice cream jack-in-the-box kite lemon monkey nickel orange pigeon quarter Roo star tree umbrella violin world xylophone zebra.";
				});
addChild(alphabetButton);
alphabetButton.visible = false;

var moreOptions:Button = new Button();
moreOptions.width=toolWidth-datagridPadding*2;
moreOptions.move(datagridPadding + (widthDif / 2), labelField.y*4 + labelField.height + debugText.height*2);
moreOptions.label = "Add Hidden Tools";
moreOptions.addEventListener(MouseEvent.CLICK, function() {
		//myDataProvider.addItem( { label:"Collocating Words" , url:"" } );	
		myDataProvider.addItem( {label:"Old Word Cloud", url:endPoint + "listwords"});
		myDataProvider.addItem( { label:"Old List Words", url:endPoint + "listwords"});
		myDataProvider.addItem( { label:"Old Get Concordance", url:allOptions["endPoint"] + "concordance" } );
		myDataProvider.addItem( { label:"Old Collocating Words" , url:allOptions["endPoint"] + "collocates" } );
		
		if (myDataProvider.length*20 < toolHeight-40) {
			ListTools.height=myDataProvider.length*20;
		} else {
			ListTools.height= 200*Math.floor((toolHeight-40)/20);
		}
		
	});
addChild(moreOptions);
moreOptions.visible = false;			


function reportKeyDown(event:KeyboardEvent):void {
	if (event.charCode==126) {
		if (labelField.visible == false) {
			labelField.visible = true;
			debugText.visible = true;
			moreOptions.visible = true;
			alphabetButton.visible = true;
			labelField.htmlText="pageURL: " 
			+ pageURL
			+ "<br />allOptions: "
			+ allOptions
			//+ "<br />htmlTag: "
			//+ allOptions["htmlTag"]
			+ "<br />backgroundColor: "
			+ allOptions["background-color"]
			+ "<br />defaultStopList: "
			+ allOptions["stopList"]
			+ "<br /><br />htmlText: "
			+ htmlText
			+ "<br /><br />src: "
			+ src
			+ "<br /><br />TAPORresult: "
			+ TAPORresult
			+ "<br /><br />variables.htmlInput: "
			+ variables.htmlInput;
			
			
			
		} else if (labelField.visible==true) {
			labelField.visible = false;
			debugText.visible = false;
			moreOptions.visible = false;
			alphabetButton.visible = false;
			
			if (tool.backg.contains(patternSelect)) {
				tool.backg.removeChild(patternSelect);
			}
			if (tool.backg.contains(patternField)) {
				tool.backg.removeChild(patternField);
			}
		}
	}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
}
function link(text:String, url:String) {
return "<a href='" + url + "'><font size='+2' color='"+allOptions["link-color"]+"'><b>" + text + "</b></font></a>";
}
function placeBelow(objectAbove) {
return objectAbove.y+objectAbove.height+datagridPadding;
}
function setLabelText(textField:TextField, textString:String, yPos:int, parent:Object) {
textField.width=toolWidth-datagridPadding*2;
textField.x=datagridPadding+(widthDif/2);
textField.y=yPos;
textField.selectable=false;
textField.defaultTextFormat=labelFormat;
textField.antiAliasType=AntiAliasType.ADVANCED;
textField.htmlText=textString;
parent.addChild(textField);
}
function placeComponent(component, textField:TextField, textString:String, yPos:int, parent:Object) {
setLabelText(textField, textString, yPos, parent);
component.move(datagridPadding+(widthDif/2),textField.y+textField.textHeight+3);
component.width=toolWidth-datagridPadding*2;
parent.addChild(component);
}
function categoryFirstRun() {
for (var catHeadName:String in CategoriesArray) {
	CategoriesArray[catHeadName].object.y=toolEnd+(CategoriesArray[catHeadName].position-1)*distanceBetween;
	catSetup(CategoriesArray[catHeadName].object, CategoriesArray[catHeadName].title, CategoriesArray[catHeadName].position-1, CategoriesArray[catHeadName].tool);
}
slidingSetup (CategoriesArray["Results"].position);
NavigateToURLcss();
}
function catBgVisibility(BgLoading:Boolean) {
Loading.visible=BgLoading;
}
function catSetup(object:Object, barText:String, barNumber:Number, clickFunction) {
object.y=toolEnd+(distanceBetween*barNumber);
object.catBar.catTXT.text = barText;
object.catBar.catTXT.x = 9.8 + widthDif / 2;
object.openArrow.x = 3.0 + widthDif / 2;
object.catBar.useHandCursor=true;
object.catBar.addEventListener(MouseEvent.CLICK,clickFunction);
object.catBar.catShading.width=toolWidth;
object.catBar.catShading.x=widthDif/2;
}
function openTools(event:MouseEvent):void {
		trace("opening Tools");
slidingSetup(CategoriesArray["tool"].position);
}
function openOptions(event:MouseEvent):void {
slidingSetup(CategoriesArray["options"].position);
}
function openadvOptions(event:MouseEvent):void {
slidingSetup(CategoriesArray["advOptions"].position);
}
function openResults(event:MouseEvent):void {
	trace("opening Results");
slidingSetup(CategoriesArray["Results"].position);
NavigateToURLcss();
}
function openAbout(event:MouseEvent):void {
slidingSetup(CategoriesArray["about"].position);
}
function slidingSetup(position) {
//trace("works ("+position+")");
for (var catHeadName:String in CategoriesArray) {
	//CategoriesArray[catHeadName].object.openArrow.gotoAndStop(2);
	if (position==CategoriesArray[catHeadName].position) {//checks if the current object is the one that had been referenced by "position"
		CategoriesArray[catHeadName].object.openArrow.gotoAndStop(2);//sets the arrow if its the right object
	} else {
		CategoriesArray[catHeadName].object.openArrow.gotoAndStop(1);
	}
	if (CategoriesArray[catHeadName].position<=position) {
		slideCat(CategoriesArray[catHeadName].object, toolTop+(distanceBetween*(CategoriesArray[catHeadName].position-1)));
	} else {
		slideCat(CategoriesArray[catHeadName].object, toolEnd+(distanceBetween*(CategoriesArray[catHeadName].position-1)));
	}
	currentCat=position;
}
if (dg) {
	addChild(dg);
	removeChild(dg);
}
}
function slideCat(object:Object, yCoordinate:Number) {
	//var myTween:Tween=new Tween(object,"y",Regular.easeIn,object.y,yCoordinate, 3,false);
	//myTween.looping=false;
	
	object.y=yCoordinate;
}
function colonSplit(inputText){ //Fuction for splitting string of colon/semi-colon separated name-value pairs in object of all the values.
		var allOptionsArray:Array = inputText.split(/;/g);
		var allOptionsObject:Object = new Object();
		for (i; i < allOptionsArray.length; i++) {
			var tempOptions:Array = allOptionsArray[i].split(/:/);
			//to account for the colon in a url, and to remove the "url(" and ")", which are only included for aesthetic and user reasons.
			if (tempOptions[0]=="endPoint") {
				while (tempOptions.length>2) { 
					tempOptions[tempOptions.length-2] = tempOptions[tempOptions.length-2]+":"+tempOptions[tempOptions.length-1];
					tempOptions.pop();
				}
				tempOptions[1]=tempOptions[1].replace(/url\(|\)/g, "");
				trace (tempOptions.toString(","));
			}
			if (tempOptions.length==2) {
				allOptionsObject[tempOptions[0]] = tempOptions[1];
			}
		}
		return allOptionsObject;
	}
	
	
	

